import { ref, computed } from 'vue'
import { defineStore } from 'pinia'
import { ElMessage } from 'element-plus'
import {getCaradminlist,getStatAdmin,getLineAdmin,getTicketAdmin,getOrderAdmin,getInfo} from '../api/tick'
import {getHMS} from '../utils/time'
export const useStore = defineStore('counter', {
  state:()=>{
    return {
      tickList:[],
      arr:[],
      //分页数据
      arrList:[],
      //筛选数据
      screenList:[],
      //线路数据
      linearr:[],
      //车票数据
      tickarr:[],
      //订单数据
      orderarr:[],
      //个人轨迹数据
      infoarr:[],
      
    }
  },
  actions:{
    //获取车次列表
    Init(){
      getCaradminlist().then(res=>{
        this.tickList = res.data
       // console.log(res);
      })
    },
    //获取车站列表
    StatList(){
      getStatAdmin().then(res=>{
        this.arr = res.data 
        this.arrList = res.data.slice(0,10) 
      })
    },
    //获取线路列表
    LineList(){
      getLineAdmin().then(res=>{
        this.linearr = res.data 
        this.arrList = res.data.slice(0,10) 
        // console.log(this.arrList);
      })
    },
    //获取车票列表
    TicketList(){
      getTicketAdmin().then(res=>{
        this.tickarr = res.data 
        this.arrList = res.data.slice(0,10) 
        console.log(this.arrList);
      })
    },
    //获取订单列表
    OrderList(){
      getOrderAdmin().then(res=>{
        this.orderarr = res.data 
        this.arrList = res.data.slice(0,10) 
        console.log(this.arrList);
      })
    },
    //获取个人轨迹
    InfoList(){
      getInfo().then(res=>{
        this.infoarr = res.data 
        console.log(this.infoarr[0].list);
      })
    },
    
    //新增车站
    StatAdd(formEl){
    //  console.log(formEl);
     this.arrList.unshift({
      id:formEl.id,
      city:formEl.city,
      status:formEl.status
     })
    },
    //车站搜索
    StatSearch(val){
      //  let value = String(val)
    //  this.arrList = this.arrList.filter(item=>String(item.id).includes(String(val)))
      this.arrList = this.arrList.filter(item=>item.city.includes(val))
    },
    //车站启用
    Start(flag){
      console.log(flag);
      let arr = [...this.arrList]
      if(flag == true){
        this.screenList = arr.filter(item=>Boolean(item.status))
        // console.log(this.screenList);
      }else{
        this.screenList =[]
      }
    },
    //删除车站
    DELETEITEM(inde,row){
      //  console.log(row,inde);
      this.arrList.forEach((item,index)=>{
        if(row.key == item.key){
          ElMessage({
            message: '删除成功',
            type: 'success',
          })
          
           this.arrList.splice(index, 1)
        }
       })
    },
    //编辑车站
    EDITITEM(row){
        console.log(row);
        let arr = JSON.parse(JSON.stringify(this.arrList))
        this.arrList.forEach((item,index)=>{
          if(row.key == item.key){
            arr.splice(index,1,row)
          }
        })
        this.arrList = arr
    },
    //车站分页
    PageSize(val){
      console.log(val);
      this.arrList =  this.arr.slice((val-1)*10,val*10)
      console.log(this.arr);
    },
    //车次列表搜索
    edit(vehicle,all){
      // console.log(vehicle,all,this.tickList);
      this.tickList = this.tickList.filter(item=>item.vehicle.includes(vehicle))   
    },
    //车次列表增加
    addItem(form){
      console.log(form);
      this.tickList.unshift({
        id: form.id,
        train: form.train,
        vehicle: form.vehicle,
        all: form.all,
        carriage: form.carriage,
        num: form.num,
        date: getHMS(form.date),
        status: form.status
      })
    },
    //车次列表删除
    delItem(form){
      // console.log(form);
     this.tickList.forEach((item,index)=>{
      //  console.log(item);
      if(item.id == form.id){
        // console.log(item.id,form.id);
        ElMessage({
          message: '删除成功',
          type: 'success',
        })
       item.splice(index, 1)
      }
   
     })
    }
  }
})
